package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/callBarring/model"
	"strconv"
)

type CallBarrringController struct {
	Ctx iris.Context
	Customer *model.Customer
}

func (cbc *CallBarrringController)PostCallout() mvc.Result {

	cbc.Customer = new(model.Customer)

	accountIdStr := cbc.Ctx.FormValue("accountId")
	accountId, err := strconv.ParseInt(accountIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	customerIdStr := cbc.Ctx.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	callTypeStr := cbc.Ctx.FormValue("callType")
	callType, err := strconv.Atoi(callTypeStr)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	callStateStr := cbc.Ctx.FormValue("callState")
	callState, err := strconv.Atoi(callStateStr)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	callTimeStr := cbc.Ctx.FormValue("callTime")
	callTime, err := strconv.ParseInt(callTimeStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := cbc.Customer.CallOut(accountId, customerId, callType, callState, callTime)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cbc *CallBarrringController)PostQueryuserinfo() mvc.Result {

	cbc.Customer = new(model.Customer)

	customerIdStr := cbc.Ctx.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := cbc.Customer.QueryUserInfo(customerId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}


















